<template>
<div>
  <!-- 面包屑导航 -->
  <el-breadcrumb separator-class="el-icon-arrow-right" style="margin-bottom: 20px;">
    <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
    <el-breadcrumb-item :to="{ path: '/blog/list' }">博客管理</el-breadcrumb-item>
    <el-breadcrumb-item>标签管理</el-breadcrumb-item>
  </el-breadcrumb>
  <el-button size="small" type="primary" icon="el-icon-plus" @click="handleAdd" style="margin-bottom: 20px;">添加</el-button>
  <!--列表-->
  <el-table :data="listData" border style="width: 100%" stripe>
    <el-table-column align="center" label="序号" width="120" type="index"></el-table-column>
    <el-table-column align="center" prop="name" label="标签名称" width="300"></el-table-column>
    <el-table-column align="center" prop="categoryName" label="所属分类" width="120">
      <template slot-scope="scope">
        <el-tag type="primary">{{scope.row.categoryName}}</el-tag>
      </template>
    </el-table-column>
    <el-table-column align="center" prop="updateTime" label="更新时间" width="300">
      <template slot-scope="scope">
        {{getFormat(scope.row.updateTime)}}
      </template>
    </el-table-column>
    <el-table-column align="center" prop="createTime" label="创建时间" width="300">
      <template slot-scope="scope">
        {{getFormat(scope.row.createTime)}}
      </template>
    </el-table-column>
    <el-table-column align="center" label="操作" min-width="300">
      <template slot-scope="scope">
<!--        <el-button icon="el-icon-setting" type="primary" size="mini"  >分配标签</el-button>-->
        <el-button icon="el-icon-edit" type="success" size="mini" @click="handleEdit(scope.row.labelId)">编辑</el-button>
        <el-button icon="el-icon-document-delete" type="danger" size="mini" @click="handleDel(scope.row.labelId)">删除</el-button>
      </template>
    </el-table-column>
  </el-table>

  <!-- 新增或者编辑的弹窗 -->
  <edit :title="edit.title" :isEdit="edit.isEdit" :labelId="edit.labelId" :visiable="edit.visiable" :formData="edit.formData" :remoteClose="remoteClose"/>
</div>
</template>

<script>
import blogApi from '@/api/blog';
import {dateFormat} from "@/utils/date";
import Edit from '@/views/label/edit';
export default {
  components: {
    Edit
  },
  data() {
    return {
      listData: [], // 标签信息列表
      edit: {
        title: '',
        labelId: '',
        visiable: false,
        formData: {},
        isEdit: false
      },
    }
  },

  created() {
    this.fetchData();
  },

  methods: {
    fetchData() {
      blogApi.getAllLabel().then(response => {
        if (response.code === 200) {
          this.listData = response.data;
        }
      })
    },

    // 删除标签
    handleDel(id) {
      this.$confirm('确认删除这条记录吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // 发送删除请求
        blogApi.deleteLabel(id).then(response => {
          if (response.code === 200) {
            this.$notify({
              title: '成功',
              message: '删除成功',
              type: 'success',
              duration: 1500
            });
          } else if (response.code === 306) {
            this.$notify.error({
              title: '错误',
              message: '您没有操作权限，请联系管理员',
              duration: 1500
            });
          } else {
            this.$notify.error({
              title: '错误',
              message: '删除失败',
              duration: 1500
            });
          }
          this.fetchData();
        })
      }).catch(() => {
        // 取消删除
      });
    },

    // 添加标签
    handleAdd() {
      this.edit.visiable = true;
      this.edit.title = '添加';
      this.edit.isEdit = false;
    },

    // 编辑标签信息
    handleEdit(id) {
      blogApi.getLabelById(id).then(response => {
        if (response.code === 200) {
          this.edit.title = '编辑';
          this.edit.labelId = id;
          this.edit.visiable = true;
          this.edit.formData = response.data;
          this.edit.isEdit = true;
        } else {
          this.$notify.error({
            title: '错误',
            message: '系统异常，请稍后再试',
            duration: 1500
          });
        }
      })
    },

    // 子组件会触发该方法来关闭窗口
    remoteClose() {
      this.edit.title = '';
      this.edit.categoryId = '';
      this.edit.visiable = false;
      this.edit.isEdit = false;
      this.fetchData();
    },

    // 格式化日期
    getFormat(date){
      return dateFormat(date);
    },
  },

}
</script>

<style scoped>

</style>
